//
//  LspScrollView.m
//  Product_A
//
//  Created by lanou on 16/5/12.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "LspScrollView.h"
#import <UIImageView+WebCache.h>
@interface LspScrollView() <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSArray *tempArray;
@property (nonatomic, assign) CGRect theFrame;
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) NSTimer *timer;
@end

@implementation LspScrollView 

- (instancetype)initWithframe:(CGRect)frame array:(NSArray *)array {
    self = [super initWithFrame:frame];
    _timeInterval = 1;
    if (self) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.scrollView.contentSize = CGSizeMake(frame.size.width * (array.count + 2), frame.size.height);
        self.scrollView.contentOffset = CGPointMake(frame.size.width * 1, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        for (int i = 0; i < array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * (i + 1), 0, frame.size.width, _scrollView.frame.size.height)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:array[i]]];
            [self.scrollView addSubview:imageView];
        }
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height * 4 / 5, frame.size.width, 30)];
        self.page.numberOfPages = array.count;
        self.page.pageIndicatorTintColor = [UIColor blackColor];
        self.page.currentPageIndicatorTintColor = [UIColor redColor];
        [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_page];
        
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * 0, 0, frame.size.width, frame.size.height)];
        [leftImageView sd_setImageWithURL:[NSURL URLWithString:array.lastObject]];
        [self.scrollView addSubview:leftImageView];
        
        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * (array.count + 1), 0, frame.size.width, frame.size.height)];
        [rightImageView sd_setImageWithURL:[NSURL URLWithString:array.firstObject]];
        [self.scrollView addSubview:rightImageView];
        // 给全局数组赋值，后面需要用
        self.tempArray = array;
        // 给全局frame赋值
        self.theFrame = frame;
 
        self.index = 1;
    }
    return self;
}

// 重写set方法,启动定时器
- (void)setStartTimer:(BOOL)startTimer {
    _startTimer = startTimer;
    // 一进来，先关闭上一次启动的定时器（不管上一次是否关闭，都在这里执行一次关闭操作）
    [_timer invalidate];
    _timer = nil;
    if (self.startTimer) {
        // 定时器
        _timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        [_timer fire];
    }
}

- (void)setTimeInterval:(NSInteger)timeInterval {
    _timeInterval = timeInterval;
    self.startTimer = YES;
}

// scollView滑动结束时调用的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = self.scrollView.contentOffset.x / _theFrame.size.width;
    if (index == 0) {
        [scrollView setContentOffset:CGPointMake(_theFrame.size.width * self.tempArray.count, 0)];
    }
    if (index == self.tempArray.count + 1) {
        [scrollView setContentOffset:CGPointMake(_theFrame.size.width * 1, 0)];
    }
    self.page.currentPage = scrollView.contentOffset.x / _theFrame.size.width - 1;
}

// 定时器触发方法
- (void)timerAction:(NSTimer *)sender {
    [self.scrollView setContentOffset:CGPointMake(_theFrame.size.width * _index, 0)];
    if (_index == 0) {
        [_scrollView setContentOffset:CGPointMake(_theFrame.size.width * self.tempArray.count, 0)];
        // 让_index等于当前偏移量
        _index = _scrollView.contentOffset.x / 375;
    }
    if (_index == self.tempArray.count + 1) {
        [_scrollView setContentOffset:CGPointMake(_theFrame.size.width * 1, 0)];
         // 让_index等于当前偏移量
        _index = _scrollView.contentOffset.x / 375;
    }
    self.page.currentPage = _scrollView.contentOffset.x / _theFrame.size.width - 1;
    
    _index++;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.isDecelerating) {
        // 用户拖拽的时候停止定时器
        [_timer setFireDate:[NSDate distantFuture]];
    }
}

- (void)pageAction:(UIPageControl *)page {
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * _theFrame.size.width, 0)];
}



@end
